package com.swak.frame.util;

import java.net.InetAddress;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 
 * ClassName: AbstractUUIDGenerator.java
 * 
 * @Description: UUID
 * @author colley.ma
 * @date 2021年3月12日
 */
public abstract class AbstractUUIDGenerator {

	private static final int IP;
	static {
		int ipadd;
		try {
			ipadd = toInt(InetAddress.getLocalHost().getAddress());
		} catch (final Exception e) {
			ipadd = 0;
		}
		IP = ipadd;
	}

	public static int toInt(final byte[] bytes) {
		int result = 0;
		for (int i = 0; i < 4; i++) {
			result = (result << 8) - Byte.MIN_VALUE + bytes[i];
		}
		return result;
	}

	private static AtomicInteger counter = new AtomicInteger(0);
	private static final int JVM = (int) (System.currentTimeMillis() >>> 8);

	public AbstractUUIDGenerator() {
	}

	protected int getJVM() {
		return JVM;
	}

	protected  short getCount() {
		int countNum = counter.addAndGet(1);
		if (countNum > Short.MAX_VALUE || countNum < 0) {
			counter.set(0);
			return 0;
		}
		return (short) countNum;
	}

	protected int getIP() {
		return IP;
	}

	protected short getHiTime() {
		return (short) (System.currentTimeMillis() >>> 32);
	}

	protected int getLoTime() {
		return (int) System.currentTimeMillis();
	}
}
